package com.yoshon.yoshonapp.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期转换
 *
 * @author tan
 *
 */
public class DateUtils {
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
	public static final String DATE_MMddHHmm = "MM-dd HH:mm";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final DateFormat SNS_DATE_FORMAT = new SimpleDateFormat(
			"M月d日HH:mm");
	public static final DateFormat SNS_TODAY_FORMAT = new SimpleDateFormat(
			"HH:mm");
	public static final DateFormat SNS_YESTERDAY_FORMAT = new SimpleDateFormat(
			"昨天 HH:mm");
	public static final DateFormat SNS_BEFORE_YESTERDAY_FORMAT = new SimpleDateFormat(
			"前天 HH:mm");
	public static final DateFormat SNS_THIS_YEAR_FORMAT = new SimpleDateFormat(
			"MM-dd HH:mm");
	public static final DateFormat SNS_BEFORE_THIS_YEAR_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd");
	public static final DateFormat SNS_SIMPLE_FORMAT = new SimpleDateFormat(
			"yyMMdd");
	public static final DateFormat DATE_FORMAT_YYYYMM = new SimpleDateFormat(
			"yyMM");
	public static final DateFormat SNS_MONTH_DAY_FORMAT = new SimpleDateFormat(
			"M月d日");

	public static DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 在当前时间基础上加分钟
	 *
	 * @param date
	 *            基础时间
	 * @param days
	 *            所要加的天数
	 * @param hour
	 *            所要加的小时数
	 * @param minute
	 *            所要加的分钟数
	 * @return
	 */
	public static Date addTime(Date date, int days, int hour, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, days);
		calendar.add(Calendar.HOUR, hour);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	/***
	 *
	 * @param date
	 * @param months
	 * @return
	 */
	public static String addMonth(Long date, int months) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(date));
		calendar.add(Calendar.MONTH, months);
		return dateToString(calendar.getTime().getTime(), "yyyy年MM月dd日");
	}

	/**
	 * 获得某一天的最小间
	 *
	 * @param date
	 *            时间
	 * @return 当天最时间
	 */
	public static Date getMinOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获得某一天的最大时间
	 *
	 * @param date
	 *            时间
	 * @return 当天最时间
	 */
	public static Date getMaxOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 *
	 *
	 * @param date
	 *            日期或时间
	 * @param pattern
	 *            格式
	 * @return
	 */
	public static Date stringToDate(String date, String pattern) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern,
				Locale.CHINESE);
		Date d = null;
		try {
			d = simpleDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 将long毫秒时间转换为data XXXX-XX-XX XX:XX
	 *
	 * Date
	 */
	public static Date LongToDate(Long date, String pattern) {
		Date date1 = new Date();
		if (date != null && date != 0) {
			date1.setTime(date);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					DATE_FORMAT, Locale.CHINESE);
			simpleDateFormat.applyPattern(pattern);
		}

		return date1;
	}

	/**
	 * 日期转换成毫秒
	 *
	 * @param date
	 *            日期或时间
	 * @param pattern
	 *            格式
	 * @return
	 */
	public static Long stringToLong(String date, String pattern) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern,
				Locale.CHINESE);
		Date date1 = null;
		try {
			if (StringUtils.isNotBlank(date)) {
				date1 = simpleDateFormat.parse(date);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (date1 == null ? 0L : date1.getTime());
	}

	/**
	 * 获取当前时间并转换成毫秒数
	 *
	 * @return
	 */
	public static Long sysTimeToLong() {
		Date date = new Date();
		return date.getTime();
	}

	public static String dateToString(Long date, String pattern) {
		Date date1 = new Date();
		String timeStr = "";
		if (date != null && date != 0) {
			date1.setTime(date);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("",
					Locale.CHINESE);
			simpleDateFormat.applyPattern(pattern);
			timeStr = simpleDateFormat.format(date1);
		}
		return timeStr;
	}

	/**
	 * 计算倒计时
	 *
	 * @param strDate
	 *            计算日期
	 * @param pattern
	 *            日期格式 (比如 yyyy/MM/dd)
	 * @return 倒计时天数
	 */
	public static Long countDown(String strDate, String pattern) {
		Long longDate = stringToLong(strDate, pattern);
		Long longNow = stringToLong(dateToString(sysTimeToLong(), pattern),
				pattern);
		Long countDown = (longDate - longNow) / 1000 / 3600 / 24;
		return countDown;
	}

	/**
	 * 获取“今天”零点的毫秒数
	 *
	 * @return
	 */
	public static Long getBeginOfToday() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTimeInMillis();
	}

	/**
	 * 获得某天的开始时间
	 *
	 * @param date
	 * @return
	 */
	public static Date getBeginOfDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获得某天的开始时间
	 *
	 * @param date
	 * @return
	 */
	public static Date getBeginOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_YEAR, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 返回两者之间的分钟差
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int subMinute(Date date1, Date date2) {
		Long subTime = date1.getTime() - date2.getTime();
		if (subTime < 0) {
			subTime = date2.getTime() - date1.getTime();
		}
		Long subMinute = subTime / 60000;
		return subMinute.intValue();
	}

	public static boolean isMonthFirst() {
		Calendar localTime = Calendar.getInstance();
		int x = localTime.get(Calendar.DAY_OF_MONTH);
		return x == 1 ? true : false;
	}

	/**
	 * 取本月最后一天
	 *
	 * @return
	 */
	public static int getLastDayOfMonth(Long date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(LongToDate(date, "yyyy-MM-dd"));
		// 某年某月的最后一天
		return cal.getActualMaximum(Calendar.DATE);
	}

	/***
	 * 获取当前日期偏移后的时间的毫秒数
	 *
	 * @author scott.wan
	 * @param day
	 * @return
	 */
	public static long getCurrDateOffsetTime(int day) {
		Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTimeInMillis();
	}

	/**
	 * 社交格式日期为yyMMdd的形式
	 *
	 * @param date
	 * @return
	 */
	public static String formatSimpleDate(Date date) {
		return SNS_SIMPLE_FORMAT.format(date);
	}

	/**
	 * 格式化日期类型
	 *
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		if (date == null)
			return null;
		SimpleDateFormat f = new SimpleDateFormat(format);
		return f.format(date);
	}

	/**
	 * 格式化字符串 formatDate
	 *
	 * @param date
	 *            日期
	 * @return 返回格式化后的日期字符串
	 */
	public static String formatDateTime(Date date) {
		DateFormat DATE_TIME_FORMAT = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		if (date == null) {
			return null;
		} else {
			return DATE_TIME_FORMAT.format(date);
		}
	}

	/**
	 * 抽取社区计算时间的方法
	 *
	 * @author 李军 2013.1.27
	 */
	public static String formatDateToHours(Date date, String format) {
		if (date != null) {
			Date crrDate = new Date();
			if (date.after(DateUtils.addTime(crrDate, 0, 0, -1))) {
				return "刚刚";
			} else if (date.after(DateUtils.addTime(crrDate, 0, -1, 0))) {
				return DateUtils.subMinute(crrDate, date) + "分钟前";
			} else if (date.after(DateUtils.getBeginOfDate(crrDate))) {
				return DateUtils.SNS_TODAY_FORMAT.format(date);
			} else {
				if (StringUtils.isNotBlank(format)) {
					return formatDate(date, format);
				} else {
					return DateUtils.SNS_DATE_FORMAT.format(date);
				}
			}
		}
		return null;
	}

	/**
	 * 抽取社区计算时间的方法
	 *
	 * @author 李军 2013.1.27
	 */
	public static String formatDateToHours(Long time, String format) {
		if (null == time) {
			return "";
		}
		Date date = new Date(time);
		if (date != null) {
			Date crrDate = new Date();
			if (date.after(DateUtils.addTime(crrDate, 0, 0, -1))) {
				return "刚刚";
			} else if (date.after(DateUtils.addTime(crrDate, 0, -1, 0))) {
				return DateUtils.subMinute(crrDate, date) + "分钟前";
			} else if (date.after(DateUtils.getBeginOfDate(crrDate))) {
				return DateUtils.SNS_TODAY_FORMAT.format(date);
			} else {
				if (StringUtils.isNotBlank(format)) {
					return formatDate(date, format);
				} else {
					return DateUtils.SNS_DATE_FORMAT.format(date);
				}
			}
		}
		return null;
	}

	/**
	 * 格式化yyyy-MM-dd的日期
	 *
	 * @param date
	 * @return
	 */
	public static Date parseDate(String date) {
		try {
			return DATEFORMAT.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 计算两个日期相差多少天
	 *
	 * @param begin
	 *            起始日期
	 * @param end
	 *            结束日期
	 * @return 返回相差的天数
	 */
	public static int countDays(String begin, String end) {
		int days = 0;
		Calendar c_b = Calendar.getInstance();
		Calendar c_e = Calendar.getInstance();
		c_b.setTime(parseDate(begin));
		c_e.setTime(parseDate(end));

		if (c_b.after(c_e)) {
			return -1;
		} else {
			while (c_b.before(c_e)) {
				days++;
				c_b.add(Calendar.DAY_OF_YEAR, 1);
			}
		}
		return days;
	}

	public static String parseDate(Date time) {
		if (time != null) {
			Date date = new Date();
			if (time.after(DateUtils.addTime(date, 0, 0, -1))) {
				return "刚刚";
			} else if (time.after(DateUtils.addTime(date, 0, -1, 0))) {
				return DateUtils.subMinute(date, time) + "分钟前";
			} else if (time.after(DateUtils.getBeginOfDate(date))) {
				return DateUtils.SNS_TODAY_FORMAT.format(time);
			} else {
				return DateUtils.SNS_DATE_FORMAT.format(time);
			}
		}
		return "";
	}

	// -------------------------------------------------------------------
	public static String getShortTime(long dateline) {
		String shortstring = null;
		String time = timestampToStr(dateline);
		Date date = getDateByString(time);
		if (date == null)
			return shortstring;

		long now = Calendar.getInstance().getTimeInMillis();
		long deltime = now - date.getTime();

		if (deltime > 24 * 60 * 60 * 7)// 一周前
		{
			SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
			Calendar calendar = Calendar.getInstance();

			calendar.setTimeInMillis(dateline);
			shortstring = formatter.format(calendar.getTime());
		} else if (deltime > 24 * 60 * 60) {
			shortstring = (int) (deltime / (24 * 60 * 60)) + "天前";
		} else if (deltime > 60 * 60) {
			shortstring = (int) (deltime / (60 * 60)) + "小时前";
		} else if (deltime > 60) {
			shortstring = (int) (deltime / (60)) + "分前";
		} else if (deltime > 1) {
			shortstring = deltime + "秒前";
		} else {
			shortstring = "1秒前";
		}
		return shortstring;
	}

	public static Date getDateByString(String time) {
		Date date = null;
		if (time == null)
			return date;
		String date_format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat format = new SimpleDateFormat(date_format);
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	public static String timestampToStr(long dateline) {
		Timestamp timestamp = new Timestamp(dateline);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定义格式，不显示毫秒
		return df.format(timestamp);
	}

	public static Date str2Date(String str, String format) {
		if (str == null || str.length() == 0) {
			return null;
		}
		if (format == null || format.length() == 0) {
			format = DATE_TIME_FORMAT;
		}
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			date = sdf.parse(str);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;

	}

	public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
		return date2Str(c, null);
	}

	public static String date2Str(Calendar c, String format) {
		if (c == null) {
			return null;
		}
		return date2Str(c.getTime(), format);
	}

	public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
		return date2Str(d, null);
	}

	public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
		if (d == null) {
			return null;
		}
		if (format == null || format.length() == 0) {
			format = DATE_TIME_FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String s = sdf.format(d);
		return s;
	}

	public static String getCurDateStr() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
				+ c.get(Calendar.DAY_OF_MONTH) + "-"
				+ c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
				+ ":" + c.get(Calendar.SECOND);
	}

	/**
	 * 获得当前日期的字符串格式
	 *
	 * @param format
	 * @return
	 */
	public static String getCurDateStr(String format) {
		Calendar c = Calendar.getInstance();
		return date2Str(c, format);
	}

	// 格式到秒
	public static String getMillon(long time) {

		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

	}

	// 格式到天
	public static String getDay(long time) {

		return new SimpleDateFormat("yyyy-MM-dd").format(time);

	}

	// 格式到毫秒
	public static String getSMillon(long time) {

		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

	}

	public static String getMonthDay(String time) {
		Date date = null;
		date = getDateByString(time);
		if(date != null){
			return DateUtils.SNS_MONTH_DAY_FORMAT.format(date);
		}else {
			return "";
		}
	}
	public static String getMinuteSecond(String time) {
		Date date = null;
		date = getDateByString(time);
		if(date != null){
			return DateUtils.SNS_TODAY_FORMAT.format(date);
		}else {
			return "";
		}
	}
	public static String getPeriod(String startTime,String endTime) {
		String monthDay = getMonthDay(startTime);
		String start = getMinuteSecond(startTime);
		String end = getMinuteSecond(endTime);
		return monthDay + " " + start + "~" + end;
	}

	public static String getGenseeMillon(long time) {

		return new SimpleDateFormat("HH:mm:ss").format(time);

	}

	//转化当前年月日
	public static String formatTime(int t){
		return t>=10? ""+t:"0"+t;//三元运算符 t>10时取 ""+t
	}

}
